package cn.wolfcode.controller;

import cn.wolfcode.domain.User;
import cn.wolfcode.query.UserQuery;
import cn.wolfcode.redis.IUserInfoRedisService;
import cn.wolfcode.service.IUserService;
import cn.wolfcode.util.JsonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
* 用户信息控制层
*/
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;


    @PostMapping("/login")
    public Object login(String username,String password) {
        //服务器接收请求,获取账号和密码,通过账号密码查询数据库返回user 对象
        User user = userService.login(username,password);
        //如果存在,创建令牌(随机码 token)
        String token = userInfoRedisService.setToken(user);
        //将 token 跟 user 对象封装到jsonResult 对象,并相应回浏览器
        HashMap<String, Object> map = new HashMap<>();
        map.put("user",user);
        map.put("token",token);
        return JsonResult.success(map);
    }
}
